import 'dart:math';

import 'package:flutter/material.dart';
import 'global/theme.dart';

const Categorys = ['数据总览', '平台详情', '排名统计'];
const Color notSelectTitleColor = Color(0xFFB18886);
const Color selectTitleColor = Colors.white;

enum ErrorType {
  noData,
  noNetwork,
  invalidData
}

//上方总分类切换组件
class SwitchBar extends StatelessWidget {
  final void Function(int) onClick;
  final int currentIndex;
  final String title;
  const SwitchBar({this.currentIndex, this.onClick, this.title ,Key key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.fromLTRB(5, 0, 5, 10),
      child: Container(
        height: 50.0,
        color: BarColor,
        child: Row(
          children: <Widget>[
            /*Image(
              image: AssetImage('images/chart_logo.png'),
            ),*/
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('当前时间', style: TextStyle(color: Colors.white, fontSize: 12),),
                Text(title, style: TextStyle(color: Colors.white, fontSize: 12),),
              ],
            ),
            Expanded(
              child: Row(
                  children: Categorys.map((e) {
                int idx = Categorys.indexOf(e);
                bool isCurrent = idx == currentIndex;
                return Expanded(
                  flex: 1,
                  child: Center(
                      child: DecoratedBox(
                    child: Container(
                      child: Center(
                        child: Container(
                          width: 100,
                          height: 35,
                          child: OutlineButton(
                            onPressed: () {
                              this.onClick(idx);
                            },
                            child: Text(
                              e,
                              style: TextStyle(
                                  color: isCurrent
                                      ? selectTitleColor
                                      : notSelectTitleColor),
                            ),
                          ),
                          decoration: !isCurrent
                              ? BoxDecoration(
                                  color: Color(0xFF85513F),
                                  border:
                                      Border.all(width: 1, color: Colors.white))
                              : null,
                        ),
                      ),
                      width: 100,
                    ),
                    decoration: BoxDecoration(
                        border: isCurrent
                            ? Border(
                                bottom:
                                    BorderSide(width: 2, color: Colors.cyan))
                            : null),
                  )),
                );
              }).toList()),
            )
          ],
        ),
      ),
    );
  }
}

class SmallInfoView extends StatelessWidget {
  const SmallInfoView({this.viewInfo, this.icon, Key key}) : super(key: key);
  final List viewInfo;
  final String icon;
  static const TextStyle _textStyle = const TextStyle(
    fontSize: 15,
    color: Color(0xFF758BB5),
  );
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Image(
          image: AssetImage(icon),
          width: 60,
          height: 60,
        ),
        Container(
          child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: Center(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                      children: viewInfo.map((e) {
                return Row(
                  children: <Widget>[
                    Text(
                      e['title'],
                      style: _textStyle,
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Text(
                      e['content'],
                      style: _textStyle,
                    )
                  ],
                );
              }).toList()))),
        )
      ],
    );
  }
}

class FilterChooser extends StatelessWidget {
  const FilterChooser(
      {Key key, @required this.filterItems, this.onClick, this.currentIndex = 0})
      : super(key: key);
  final void Function(int) onClick;
  final int currentIndex;
  final List filterItems;
  @override
  Widget build(BuildContext context) {
    return Row(
        children: filterItems.map((e) {
      int idx = filterItems.indexOf(e);
      bool isSelect = idx == currentIndex;
      return GestureDetector(
        onTap: (){
          if(onClick != null){
            onClick(idx);
          }
        },
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 5),
          decoration: BoxDecoration(
              border: Border(right: BorderSide(width: 1, color: Color(0xFF758BB5)))),
          height: 20,
          child: Text(
            e,
            style: TextStyle(color: isSelect? Colors.white: Color(0xFF758BB5)),
          ),
      ));
    }).toList());
  }
}

//加载动画
class LoadingBlock extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: CircularProgressIndicator(),
    );
  }
}

//错误信息展示
class ErrorBlock extends StatelessWidget {
  static const imageMap = {
    ErrorType.noNetwork: 'images/error_net.png',
    ErrorType.invalidData: 'images/error_datainvalid.png',
    ErrorType.noData: 'images/error_nodata.png'
  };
  static const titleMap = {
    ErrorType.noNetwork: '网络错误',
    ErrorType.invalidData: '数据错误',
    ErrorType.noData: '暂无数据'
  };
  ErrorBlock({Key key, this.errorType}):super(key: key);
  final ErrorType errorType;
  @override
  Widget build(BuildContext context) {
    return
      Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Image(
            image: AssetImage(imageMap[errorType]),
            width: 50,
          ),
          SizedBox(
            height: 5,
          ),
          Text(titleMap[errorType], style:TextStyle(color: Color(0xFF758BB5))),
        ],
      ));
  }
}
